/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.opgea.qms.web.controller;

import com.opgea.qms.constraints.SessionConstraints;
import com.opgea.qms.domain.entities.Company;
import com.opgea.qms.domain.entities.Employee;
import com.opgea.qms.domain.entities.Login;
import com.opgea.qms.domain.model.JsonModelMap;
import com.opgea.qms.service.EmployeeService;
import com.opgea.qms.web.form.EmployeeDTO;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author Ramesh
 */
@Controller
@RequestMapping(value="/user")
public class EmployeeController {
    
    @Autowired
    private EmployeeService employeeService;
    
    
    @RequestMapping(value="create", method=RequestMethod.POST)
    public @ResponseBody
            Map<String, Object> create(EmployeeDTO employeeDTO, HttpServletRequest request){
        HttpSession session = request.getSession();
        Long companyId = (Long) session.getAttribute(SessionConstraints.COMPANY_ID.toString());
        employeeDTO.setCompanyId(companyId);

        employeeDTO = employeeService.update(employeeDTO);
        return JsonModelMap.success().data(employeeDTO.getFirstName()+" saved successfully!");
    }
    
    @RequestMapping(value="empList", method= RequestMethod.GET)
    public @ResponseBody
            Map<String, Object> getEmployeeList(HttpServletRequest request){
        HttpSession session = request.getSession();
        Long companyId = (Long) session.getAttribute(SessionConstraints.COMPANY_ID.toString());
        List<EmployeeDTO> empDTOList = employeeService.findAllByCompanyId(companyId);
        return JsonModelMap.success().data(empDTOList);
    }
}
